/*
 *  Copyright (c)  2016-NOW  Jacars Co.Ltd.
 *  All rights reserved.
 */
package com.jacars.biz.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jacars.biz.user.dao.model.JsptPost;
import com.jacars.biz.user.dao.service.JsptPostService;
import com.jacars.biz.user.service.PostService;
import com.jacars.biz.user.service.DictService;
import com.jacars.common.api.PageResult;
import com.jacars.common.utils.BeanUtil;
import com.jacars.common.utils.FuncUtil;
import com.jacars.db.support.Condition;
import com.jacars.db.support.Query;
import com.jacars.biz.user.vo.JsptPostVO;
import com.jacars.common.annotation.LoginUser;
import com.jacars.common.api.Result;
import com.jacars.common.model.JsptUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 岗位表 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping("/post")
@Api(value = "岗位表", tags = "岗位表接口")
public class PostController {


	private PostService postService;

	private DictService dictService;

	private JsptPostService jsptPostService;



	/**
	 * 自定义分页 岗位表
	 */
	@GetMapping("/postPage")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入post")
	public PageResult<JsptPostVO> page(JsptPostVO post, Query query) {
		IPage<JsptPostVO> pages = postService.selectPostPage(Condition.getPage(query), post);
		List<JsptPostVO> records = pages.getRecords();
		long total = pages.getTotal();
		return PageResult.succeed(records, total);
	}



	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入post")
	public Result<JsptPostVO> detail(JsptPost post) {
		JsptPost detail = jsptPostService.getOne(Condition.getQueryWrapper(post));
		// PostVO postVO = PostWrapper.build().entityVO(detail);

		JsptPostVO sysPostVO = Objects.requireNonNull(BeanUtil.copy(detail, JsptPostVO.class));
		String categoryName = dictService.getValue("post_category", detail.getCategory());
		sysPostVO.setCategoryName(categoryName);

		return Result.succeed(sysPostVO);
	}

	/**
	 * 分页 岗位表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入post")
	public Result<IPage<JsptPostVO>> list(JsptPost post, Query query) {
		IPage<JsptPost> pages = jsptPostService.page(Condition.getPage(query), Condition.getQueryWrapper(post));

		List<JsptPostVO> records = new ArrayList<>();

		pages.getRecords().forEach(post1 -> {
			JsptPostVO sysPostVO = Objects.requireNonNull(BeanUtil.copy(post1, JsptPostVO.class));
			String categoryName = dictService.getValue("post_category", post1.getCategory());
			sysPostVO.setCategoryName(categoryName);
			records.add(sysPostVO);
		});

		IPage<JsptPostVO> pageVo = new Page<>(pages.getCurrent(), pages.getSize(), pages.getTotal());
		pageVo.setRecords(records);


		// IPage<PostVO> postVOIPage = PostWrapper.build().pageVO(pages);
		return Result.succeed(pageVo);
	}



	/**
	 * 新增 岗位表
	 */
	@PostMapping("/create")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入post")
	public Result save(@Valid @RequestBody JsptPost post) {
		post.setTenantId(null);
		jsptPostService.save(post);
		return Result.succeed("操作成功");
	}

	/**
	 * 修改 岗位表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入post")
	public Result update(@Valid @RequestBody JsptPost post) {
		post.setTenantId(null);
		return Result.status(jsptPostService.updateById(post));
	}

	/**
	 * 删除 岗位表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public Result remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return Result.status(jsptPostService.removeByIds(FuncUtil.toLongList(ids)));
	}

	/**
	 * 下拉数据源
	 */
	@GetMapping("/select")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "下拉数据源", notes = "传入post")
	public Result<List<JsptPost>> select(String tenantId, @LoginUser(isFull = true) JsptUser jsptUser) {
		List<JsptPost> list = jsptPostService.list(Wrappers.<JsptPost>query().lambda().eq(JsptPost::getTenantId, FuncUtil.toStr(tenantId, jsptUser.getTenantId())));
		return Result.succeed(list);
	}

}
